package com.itheima.tanhua.web;

import com.itheima.tanhua.api.db.UserInfoServiceApi;
import com.itheima.tanhua.api.db.UserServiceApi;
import com.itheima.tanhua.service.MessagesService;
import com.itheima.tanhuan.pojo.db.User;
import com.itheima.tanhuan.pojo.db.UserInfo;
import com.itheima.tanhuan.vo.mongo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("messages")
public class MessagesController {
    @Autowired
    MessagesService messagesService;
    @DubboReference
    UserServiceApi userServiceApi;
    @DubboReference
    UserInfoServiceApi userInfoServiceApi;

    @GetMapping("contacts")
    public ResponseEntity contacts(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                   @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                   String keyword) {
        PageResult pr = messagesService.contacts(page,pageSize,keyword);
        return ResponseEntity.ok(pr);
    }

    /**
     * 添加好友 用户id
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        Long friendId = Long.valueOf(map.get("userId").toString());
        messagesService.contactsAdd(friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 根据环信ID查询用户详细信息
     */
    @GetMapping("userinfo")
    public ResponseEntity userinfo(@RequestParam("huanxinId") String huanxinId) {
        User byHuanxiId = userServiceApi.findByHuanxiId(huanxinId);
        UserInfo vo = userInfoServiceApi.findById(byHuanxiId.getId());
        return ResponseEntity.ok(vo);
    }

    @GetMapping("comments")
    public ResponseEntity comments(
            @RequestParam(name = "page",defaultValue = "1",required = false)Long page,
            @RequestParam(name = "pagesize",defaultValue = "10",required = false)Long pagesize){
        PageResult pr=messagesService.comments(page,pagesize);
        return ResponseEntity.ok(pr);
    }

}
